<template>
  <div class="aui-card--fill scroll-box card-box">
    <div class="mod-sys__dept pane-shell">
      <el-form :inline="true"
               :model="dataForm"
               class="align-right"
               @submit.native.prevent="getDataList()"
               @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-button type="primary"
                     plain
                     @click="syncrecord()">同步冲突修改记录</el-button>
          <el-button type="primary"
                     plain
                     v-loading="isLoading"
                     @click="sync()">同步部门</el-button>
          <el-button type="primary"
                     plain
                     icon='el-icon-plus'
                     @click="addWin()">新增</el-button>
          <!-- @click="openDialog('addOrUpdate',{list:dataList})">同步数据</el-button> -->
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                style="width: 100%;">
        <table-tree-column prop="name"
                           min-width="150"
                           label="部门名称"
                           header-align="center"></table-tree-column>
        <el-table-column prop="code"
                         label="部门编号"
                         header-align="center"
                         align="center"></el-table-column>
        <!-- <el-table-column prop="districtName"
                         label="所属地区"
                         header-align="center"
                         align="center"></el-table-column> -->
        <el-table-column prop="prName"
                         label="负责人"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="remarks"
                         show-overflow-tooltip
                         max-width="150"
                         label="备注说明"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="source"
                         label="来源"
                         header-align="center"
                         align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.source === 1">同步</span>
            <span v-if="scope.row.source === 2">新增</span>
          </template>
        </el-table-column>
        <el-table-column prop="updaterName"
                         label="更新人"
                         min-width="100"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="updateDate"
                         label="更新时间"
                         min-width="150"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="creatorName"
                         label="创建人"
                         min-width="100"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="createDate"
                         min-width="150"
                         label="创建时间"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       v-if="scope.row.source === 2&&$hasPermission('system:sysDept:update')"
                       @click="addWin(scope.row)">{{ $t('update') }}</el-button>
            <el-button type="text"
                       size="small"
                       class="danger"
                       v-if="scope.row.source === 2 && $hasPermission('system:sysDept:del')"
                       @click="deleteHandle({id:scope.row.id})">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update ref="addOrUpdate"
                     @callBack="getDataList"></add-or-update>
      <usererrorList ref="usererrorList"
                     @callBack="getDataList" />
      <usererrorPage ref="usererrorPage"
                     @callBack="getDataList" />
    </div>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import TableTreeColumn from '@/components/table-tree-column'
import AddOrUpdate from './dept-add-or-update'
import usererrorList from './user/user-errorList.vue'
import usererrorPage from './user/user-errorPage.vue'
export default {
  mixins: [mixinViewModule, minFormValidate],
  mounted () {
    this.getDataList({ paramData: {} }).then(res => {
      if (res.code !== 0) { return }
      if (this.$store.state.user.superAdmin !== 1) {
        res.data.map(item => {
          this.$set(item, 'show', '0')
        })
      }
    })
  },
  data () {
    return {
      mixinViewModuleOptions: {
        activatedIsNeed: false,
        deleteIsBatch: true,
        getDataListURL: '/system/sysDept/getDeptList',
        deleteURL: 'system/sysDept'
      },
      isLoading: false
    }
  },
  computed: {
    show () {
      return (val) => {
        return val !== '0'
      }
    }
  },
  methods: {
    syncrecord () {
      this.$refs.usererrorPage.init(2) // 1.同步用户 2.同步部门 3.同步职位
    },
    sync () {
      this.isLoading = true
      this.$http.post('system/sysDept/sync').then(({ data: res }) => {
        if (res.code !== 0) {
          this.isLoading = false
          return this.$message.error(res.msg)
        } else {
          if (res.data.syncDeptList.length > 0) {
            this.$refs.usererrorList.init(res.data)
          } else {
            this.getDataList()
            this.$message.success('同步成功')
          }
        }
        this.isLoading = false
      })
    },
    addWin (_row) {
      if (_row) {
        this.$refs.addOrUpdate.init(this.dataList, _row)
      } else {
        this.$refs.addOrUpdate.init(this.dataList)
      }
    }
  },
  components: {
    TableTreeColumn,
    AddOrUpdate,
    usererrorList,
    usererrorPage
  }
}
</script>
